angular.module('app.filters', [])

.filter('metricTypeFilter', [function () {
  return function (type) {
    var normalType = type.toLowerCase();
    var typeMap = {
      'origin': '数值分布',
      'zero': '零值异常',
      'speed': '速度指标',
      'frequency': '采集频率',
      'acceleration': '加速度',
      'arp': 'P阶回归模型符合程度'
    }
    var desc = typeMap[normalType];
    if (!desc) {
      desc = type;
    }
    return desc;
  }
}])

.filter('timeFormatter', [function () {
  return function (timestamp) {
    var date = new Date(timestamp);  // Maybe there need to multiply 1000
    return date.toLocaleString();
  }
}])

.filter('boolFormatter', [function () {
  return function (value) {
    if (value) {
      return '√';
    }
    return '×';
  }
}])

.filter('unique', [function () {
  return function (arr, field) {
    var o = {}, i, l = arr.length, r = [];
    for (i = 0; i < l; i += 1) {
      o[arr[i][field]] = arr[i];
    }
    for (i in o) {
      r.push(o[i]);
    }
    return r;
  }
}])

.filter('strictFilter', [function ($filter) {
  return function (input, predicate) {
    return $filter('filter')(input, predicate, true);
  }
}])

.filter('starFilter', [function () {
  return function (value) {
    value = value + '';
    if (value.startsWith('*')) {
      return value.substring(1);
    }
    return value;
  }
}])

.filter('lengthFilter', [function() {
    return function(value) {
        if (value.hasOwnProperty('length')) {
            return value.length;
        } else {
            return value;
        }
    }
}])

